#include "head.h"

void login_check(int net_fd, int *flag_break, char *uname){
    while(1){
        int flag_login = 0;
        //接收用户名和密码
        char password[50] = {0};
        int ret_recv = recv(net_fd, uname, 20, MSG_WAITALL); // 接收用户名
        //printf("%s, %ld\n", uname, strlen(uname));

        if(ret_recv == 0){
            printf("客户端断开连接\n");
            *flag_break = 1;
            break;
        }
        recv(net_fd, password, 50, MSG_WAITALL); // 接收密码
        //printf("%s, %ld\n", password, strlen(password));

        //判断用户名和密码是否正确
        int res_check_name = check_uname(uname);

        //如果用户名不存在
        if(res_check_name != 0){
            printf("该用户不存在，清注册！\n");
            flag_login = 1;
            send(net_fd, &flag_login, sizeof(int), MSG_NOSIGNAL);
            // 进入注册逻辑: 接收新用户名，并去数据库查看是否重复，
            while(1){
                recv(net_fd, uname, 20, MSG_WAITALL);
                recv(net_fd, password, 50, MSG_WAITALL); 
                //printf("uname = %s\n", uname);
                //printf("password = %s\n", password);  

                //char *username = strtok(uname, "\n");
                //printf("username = %s\n", username);
                
                int res_check_newname = check_uname(uname);
                if (res_check_newname == 0){
                    send(net_fd, "该用户已存在！", sizeof("该用户已存在！"), MSG_NOSIGNAL);
                    continue;
                }else{
                    // 注册后将用户信息存入数据库
                    char salt[13] = {0};
                    char passwdhash[200] = {0};
                    add_salt(salt);
                    char *encrypted = crypt(password, salt);
                    printf("哈希计算结果：%s\n", encrypted);
                    strcpy(passwdhash, encrypted+12);

                    user_info_store(uname, salt, passwdhash);
                    printf("用户信息已保存\n");
                    send(net_fd, "注册成功！", sizeof("注册成功！"), MSG_NOSIGNAL);
                    
                    break;
                }
            }
            continue;
        }

        if (res_check_name == 0) { // 用户存在则判断密码是否正确
            // 获取盐值
            char salt[1024] = {0};
            get_salt(uname, salt); 
            // 获取哈希值
            char passwdhash[1024] = {0};
            get_passwd(uname, passwdhash);  
            // 根据密码和盐值计算哈希值
            char *encrypted = crypt(password, salt);
            printf("哈希计算结果：%s\n", encrypted);
            
            char hash_result[1024];
            // 拼接字符串
            strcpy(hash_result, salt);
            strcat(hash_result, passwdhash);
            
            if(strcmp(encrypted, hash_result) != 0){
                flag_login = 2; // 哈希值不匹配
                //continue;
            }
        }
        
        printf("flag_login = %d\n", flag_login);
        send(net_fd, &flag_login, sizeof(int), MSG_NOSIGNAL);
        if(flag_login == 0){
            break;
        }
    }
}

// 获取盐值
int add_salt(char *salt){
    // 字符池
    // salt[13] = {0};
    static const char charset[] = "0123456789"
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "!@#$%^&*()_+~`|}{[]:;?><,./-=";

    srand(time(NULL));
    strcpy(salt, "$6$");

    // 随机获取8个字符
    for (int i = 3; i < 11; i++) {
        salt[i] = charset[rand() % (sizeof(charset) - 1)];
    }
    salt[11] = '$';
    salt[12] = '\0'; // 添加字符串结尾符
    printf("salt length : %ld\n", strlen(salt));
    printf("salt : %s\n", salt);
    return 0;
}

